from com import comservice as db
from flask import Blueprint, jsonify, redirect, url_for, request
import logging, requests
import sys, traceback, os, re
from utils.tokenutil import TokenUtil

logger = logging.getLogger('comview')

com = Blueprint("com", __name__, url_prefix="/api")

# 查询常见问题列表
@com.route('/chat/getChatAskList', methods=['GET'])
def getChatAskList():
    return jsonify(db.getChatAskList())

# 查询智能聊天记录
@com.route('/chat/getRobotLogList', methods=['GET'])
def getRobotLogList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.getRobotLogList(param))

# 查询回复信息
@com.route('/chat/getChatReply', methods=['GET'])
def getChatReply():
    param = {}
    param['ask'] = request.args.get('ask')
    return jsonify(db.getChatReply(param))

# 新增智能聊天记录
@com.route('/chat/addRobotLog', methods=['POST'])
def addRobotLog():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['my'] = formdata['my']
    param['msg'] = formdata['msg']
    param['type'] = formdata['type']
    return jsonify(db.addRobotLog(param))
